﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using CellarDoor.Gui;
using CellarDoor.Gui.Elements;
using CellarDoor.Gui.Animation;

namespace CellarDoor.Gui.Demo.DemoScreens
{
    public class EventScreen : BaseScreen
    {
        public EventScreen(UiManager manager)
            : base(manager)
        {
            Initialize();
        }

        private void Initialize()
        {
            BuildPanels(0, MousePressNoTargetCheck);
            BuildPanels(1, MousePressTargetCheck);
            BuildPanels(2, MousePressSetHandeled);
        }

        private void BuildPanels(int index, UiEventHandler handler)
        {
            var panel = new UiPanel(base.Panel);
            panel.Width = 580;
            panel.Height = 120;
            panel.Left = 10;
            panel.Top = 10 + index * 130;
            panel.Name = "Panel1";
            panel.Text = panel.Name;
            panel.TextAlign = UiAlignStyle.Bottom | UiAlignStyle.Left;
            panel.MousePress += handler;
            panel.Click += MouseRelease;

            panel = new UiPanel(panel);
            panel.Width = 560;
            panel.Height = 80;
            panel.Left = 10;
            panel.Top = 10;
            panel.Name = "Panel2";
            panel.Text = panel.Name;
            panel.TextAlign = UiAlignStyle.Bottom | UiAlignStyle.Left;
            panel.MousePress += handler;
            panel.Click += MouseRelease;

            panel = new UiPanel(panel);
            panel.Width = 540;
            panel.Height = 40;
            panel.Left = 10;
            panel.Top = 10;
            panel.Name = "Panel3";
            panel.Text = panel.Name;
            panel.TextAlign = UiAlignStyle.Bottom | UiAlignStyle.Left;
            panel.MousePress += handler;
            panel.Click += MouseRelease;
        }

        private void MousePressNoTargetCheck(object sender, UiEventArgs args)
        {
            var element = sender as UiElement;
            element.Text = String.Format("{0} Pressed Target={1}", element.Name, args.Target.Name);
        }

        private void MousePressTargetCheck(object sender, UiEventArgs args)
        {
            if (args.Target == sender)
            {
                var element = sender as UiElement;
                element.Text = String.Format("{0} Pressed Target={1}", element.Name, args.Target.Name);
            }
        }

        private void MousePressSetHandeled(object sender, UiEventArgs args)
        {
            var element = sender as UiElement;
            element.Text = String.Format("{0} Pressed Target={1}", element.Name, args.Target.Name);
            args.Handeled = true;
        }

        private void MouseRelease(object sender, UiEventArgs args)
        {
            var element = sender as UiElement;
            element.Text = element.Name;
        }
    }
}
